/*
 *直接更新state的多个方法的对象
 */
import Vue from 'vue'
import {
  RECEIVE_ADDRESS,
  RECEIVE_CATEGORYS,
  RECEIVE_SHOPS,
  RECEIVE_USER_INFO,
  RESET_USER_INFO,
  RECEIVE_INFO,
  RECEIVE_RATINGS,
  RECEIVE_GOODS,
  UPDATE_FOOD_COUNT,
  CLEAR_SHOP_CART,
  RECEIVE_SEARCH_SHOPS
} from './mutation-types'

export default {
  [RECEIVE_ADDRESS] (state, {address}) {
    state.address = address
  },
  [RECEIVE_CATEGORYS] (state, {categorys}) {
    state.categorys = categorys
  },
  [RECEIVE_SHOPS] (state, {shops}) {
    state.shops = shops
  },
  [RECEIVE_USER_INFO] (state, {userInfo}) {
    state.userInfo = userInfo
  },
  [RESET_USER_INFO] (state) {
    state.userInfo = {}
  },
  [RECEIVE_INFO] (state, {info}) {
    state.info = info
  },
  [RECEIVE_RATINGS] (state, ratings) {
    state.ratings = ratings
  },
  [RECEIVE_GOODS] (state, goods) {
    state.goods = goods
  },
  [UPDATE_FOOD_COUNT] (state, {isAdd, food}) {
    if (isAdd) { // 加一
      if (!food.count) { // 初次加一
        Vue.set(food, 'count', 1) //////////////////为绑定的对象数据新增一个属性数据，使用Vue.set才能保证页面及时刷新显示
        state.cartFoods.push(food)
      } else {
        food.count++
      }
    } else { // 减一
      if (food.count) {
        food.count--
        if (food.count === 0) {
          state.cartFoods.splice(state.cartFoods.indexOf(food), 1)
        }
      }
    }
  },
  [CLEAR_SHOP_CART] (state) {
    state.cartFoods.forEach(food => {
        food.count = 0
    })
    state.cartFoods = []
  },
  [RECEIVE_SEARCH_SHOPS] (state, {searchShops}) {
    state.searchShops = searchShops,
    state.shopsRequired = true
  }
}
